const pool = require('../models/db'); // 假设有一个数据库连接池
const purchaseService = require('../services/purchaseService');

const getPurchases = async (req, res) => {
  try {
    const purchases = await purchaseService.getPurchases(req.query);
    res.status(200).json(purchases);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const addPurchase = async (req, res) => {
  try {
    const newPurchase = await purchaseService.addPurchase(req.body);
    res.status(201).json(newPurchase);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const updatePurchase = async (req, res) => {
  try {
    const updatedPurchase = await purchaseService.updatePurchase(req.params.id, req.body);
    res.status(200).json(updatedPurchase);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const deletePurchase = async (req, res) => {
  try {
    await purchaseService.deletePurchase(req.params.id);
    res.status(200).json({ message: `Purchase deleted with ID: ${req.params.id}` });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const getSuggestions = async (req, res) => {
  try {
    const suggestions = await purchaseService.getSuggestions(req.query);
    res.status(200).json(suggestions);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

module.exports = {
  getPurchases,
  addPurchase,
  updatePurchase,
  deletePurchase,
  getSuggestions,
};
